{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![alt text](https://github.com/callysto/callysto-sample-notebooks/blob/master/notebooks/images/Callysto_Notebook-Banner_Top_06.06.18.jpg?raw=true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basics of Python\n",
    "\n",
    "In this notebook, we're going to go over some of the very basics of Python for interested parties. Note that knowledge of Python is *not required* to _use_ Jupyter notebooks and take part in Callysto. This section is completely optional and we can create content which doesn't require writing anything in Python. That being said, if you want to introduce Python in the classroom or if a student changes anything, it will be useful to know the basics."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Anything in a code cell after a pound sign is a comment! \n",
    "# You can type anything here and it will not be excecuted \n",
    "\n",
    "# Variables are defined with an equals sign (=)\n",
    "\n",
    "my_variable = 10             # You cannot put spaces in variable names. \n",
    "other_variable = \"some text\" # variables need not be numbers!\n",
    "\n",
    "# Print will output our variables below the cell\n",
    "print(my_variable, other_variable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Variables are also shared between cells. You can also pring words and sentences directly. \n",
    "print(my_variable, other_variable, \"We can print text directly in quotes\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# You can do mathematical operations in python\n",
    "\n",
    "x = 5\n",
    "y = 10\n",
    "\n",
    "add = x + y\n",
    "subtract = x - y\n",
    "multiply = x * y\n",
    "divide = x/y\n",
    "\n",
    "print(add, subtract, multiply, divide)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "### Exercise 1\n",
    "\n",
    "1. In the cell below, assign variable z to your name and run the cell. \n",
    "2. In the cell below, write a comment on the same line where you define z. Run the cell to make sure the comment is not changing anything.\n",
    "---\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Enter your code in the line below\n",
    "z = \"your name here\"\n",
    "\n",
    "##\n",
    "\n",
    "print(z, \"is loving Python!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loops in Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# You can also write loops with for\n",
    "\n",
    "for i in range(5): \n",
    "    print(i)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " Let's take a moment to digest the loop syntax we saw above\n",
    " \n",
    " ```python\n",
    "  \n",
    "We have defined our *iterator* and called it i. This will take values \n",
    "based on the range function that follows it\n",
    "    ^\n",
    "    |\n",
    "for i in range(10): <- note the colon\n",
    "    print(i) <- note how this line is indented relative to \"for\"\n",
    "    \n",
    "```\n",
    "\n",
    "To start our loop, we have typed `for i in range(10):`. What this is saying is \"Do something for values of i that are in values defined by `range(10)`\". In this case, range creates 10 values by starting at zero and counting to 9. Which is what we saw printed above. \n",
    "\n",
    "Also note how there is a colon after range and the print statement is indented relative to for.  This is because any text indented relative to a control structure such as a loop is contained within the control structure in Python. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "### Exercises 2\n",
    "1. In the loop in the cell below, change `range()` to loop from 0-9.\n",
    "2. Change the loop below to print out 1-10 instead of 0-9. (Hint: what happens if you type `range(1,10)`?)\n",
    "3. Execute the loop in the cell two fields below. Can you get the loop to run without errors?\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercises 2.1 and 2.2: \n",
    "\n",
    "## Change the code below for the exercise:\n",
    "for i in range(5): \n",
    "    print(i)\n",
    "##"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercises 2.3: \n",
    "sharks = ['hammerhead', 'great white', 'tiger', 'frilled', 'bullhead', 'requiem']\n",
    "\n",
    "## Change the code below for the exercise:\n",
    "for shark in sharks\n",
    "print(shark)\n",
    "##"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## If and else statements\n",
    "\n",
    "Let's build on our previous example and introduce `if` statements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(10):\n",
    "    if i == 3:\n",
    "        print(i == 3, i, \"is three\")\n",
    "    else:\n",
    "        print(i == 3, i, \"Is not three\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice how our if and else statements are indented relative to for, and the print statements inside of our if statements are indented even further.\n",
    "\n",
    "What the lines of code does above is that inside the `if` statement, it checks if the value of `i` is equal to three denoted with double equals signs `==`. Should that condition be `True`, we enter the lines of code indented relative to `if`. We then print `i==3` which is `True`, as well as `i` and some text. \n",
    "\n",
    "If `i` is _not_ equal to three, then the statement `i==3` returns `False`. We then enter the `else` clause, which we can see in the output of the code above. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python Functions\n",
    "\n",
    "You can also define functions in Python which can help simplify and organize code that requires repeated calculations. This is done using the `def` method in Python which can be thought of to mean \"define\". A simple function is defined below. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def simple_function(x):\n",
    "    # a double asterix in python is an exponent\n",
    "    return x**2\n",
    "\n",
    "simple_function(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the line above, we're defining a function with `def`, and in this case we chose to give it a name of `simple_function`. We then specified which variable our function should take as input inside of the parenthesis.  During this definition, we _decided_ to call this variable `x`. We then called the function with its name, and a value for it to use for `x`. We can use functions in combination with other things like loops, shown below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(5):\n",
    "    new_variable = simple_function(i)\n",
    "    print(new_variable, \"Look what we can do!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice how in the loop above, we decided to define a new variable based on the output of our function. We also used our loop iterator `i` as the input to our function to calculate those squares for each value from 0 to 4. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "--- \n",
    "### Exercises 3\n",
    "1. In the first cell below, change the name of the defined function to \"some_name.\"\n",
    "2. In the second cell below, change the formula to calculate the area of a rectangle correctly. \n",
    "3. In the second cell below, change the function to return \"The area of the rectangle is\" along with the calculated output.\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercises 3.1:\n",
    "\n",
    "## Change the code below for the exercise:\n",
    "def function_name(x):\n",
    "    print('His name is ' + x)\n",
    "##\n",
    "\n",
    "(some_name(z))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercises 3.2 & 3.3:\n",
    "\n",
    "def calculated_area(x, y):\n",
    "## Change the code below for the exercise:\n",
    "    print()\n",
    "##\n",
    "\n",
    "(calculated_area(3, 5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Graphing\n",
    "We can also graph our function using Python with the following code "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# this library contains some mathematical functions to use\n",
    "import numpy as np\n",
    "# this is the plotting library\n",
    "import matplotlib.pyplot as plt \n",
    "# this is a \"magic\" command which tells jupyter to display our graphs in the notebook\n",
    "%matplotlib inline\n",
    "\n",
    "\n",
    "# Here we generate points to plot our function at.\n",
    "# The first number is the minimum, the second is the maximum, and the third is how many points to generate\n",
    "\n",
    "values_to_plot = np.linspace(-10,10,100) # this creates a list of 100 points from [-10,10]\n",
    "\n",
    "# this is how we create a graph. Notice how we're using 'plt' as it is the library we imported\n",
    "# which contains the plot functions\n",
    "plt.plot(values_to_plot, simple_function(values_to_plot))\n",
    "# add some axes labels\n",
    "plt.xlabel(\"X values\") \n",
    "plt.ylabel(\"Y values\")\n",
    "# we can use latex in plots as well!\n",
    "plt.title(\"$f(x) = x^2$\") \n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "--- \n",
    "### Exercises 4\n",
    "1. The plot in the first cell below plots the points defined in the lists x & y. Change the numbers in x & y to see how it affects the graph. \n",
    "2. The plot two cells down has various lines commented out to add different features to the plot. Play around with the plot by adding in various lines. \n",
    "3. In the plot two cells down, try to add in a third line by defining a function and having it plot the function.\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercises 4.1:\n",
    "\n",
    "## Change the code below for the exercise:\n",
    "x = [0, 2, 4, 6, 8, 10]\n",
    "y = [0, 4, 8, 12, 16, 20]\n",
    "##\n",
    "\n",
    "plt.plot(x,y, marker='o',markersize=8);\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercises 4.2 & 4.3: \n",
    "values_to_plot = np.linspace(0,10,11)\n",
    "x2 = values_to_plot\n",
    "y2 = simple_function(values_to_plot)\n",
    "x3 = values_to_plot\n",
    "\n",
    "## Change the code below for exercise 4.3:\n",
    "def new_function(x):\n",
    "    return x\n",
    "y3 = new_function(values_to_plot)\n",
    "\n",
    "## Change the code below for the exercise 4.2 & 4.3:\n",
    "plt.plot(x,y)\n",
    "plt.plot(x2, y2)\n",
    "#plt.grid()\n",
    "#plt.xlabel('x label')\n",
    "#plt.ylabel('y label')\n",
    "#plt.title(\"Simple Plot\")\n",
    "#plt.legend()\n",
    "\n",
    "##"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![alt text](https://github.com/callysto/callysto-sample-notebooks/blob/master/notebooks/images/Callysto_Notebook-Banners_Bottom_06.06.18.jpg?raw=true)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
